//
//  STContentViewController.swift
//  iPhone
//
//  Created by Toj on 11/17/23.
//

import UIKit

// MARK: - 懒加载(属性), 变量, 初始化init
class STContentViewController: UIViewController {
    
    private let viewModel = STContentViewModel()
    
    /** <#NOTE#> **/
    private let toolsBarHeight: CGFloat = 44
    private lazy var toolsBar: UIView = {
        let view = UIView()
        view.backgroundColor = .random
        return view
    }()
    
    /** <#NOTE#> **/
    private lazy var tabsView: STTabsViewController = {
        let vc = STTabsViewController()
        vc.tdelegate = self
        return vc
    }()
    
    deinit {
        print("-->startimes: \(st_className) func: deinit")
    }
}

// MARK: - 生命周期
extension STContentViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupMakeInitialize()
        setupMakeAddSubviews()
        setupMakeLayoutSubviews()
        tabsRequest()
    }
}

// MARK: - 数据请求
extension STContentViewController {
    
    func tabsRequest() {
        viewModel.request { [weak self] tabs, defaultViewModel in
            if let viewModel = defaultViewModel {
                self?.tabsView.selectedIndex = viewModel.index
            }
            
            self?.tabsView.tabs = tabs
        }
    }
}

// MARK: - 点击事件/ 通知事件回调
@objc private extension STContentViewController {
    
}

// MARK: - 子类可重写方法/ objc事件
@objc extension STContentViewController {
    
}

// MARK: - Public
extension STContentViewController {
    
}

// MARK: - Private
private extension STContentViewController {
    
}

// MARK: - STTabViewScrollDelegate
extension STContentViewController: STTabsViewControllerDelegate {
    func tabsViewController(_ viewController: STTabsViewController, scrollView offsetY: CGFloat) {
        // print("-->startimes: offsetY: \(offsetY)")
        // 上滑: offsetY > 0
        var toolsFrame = toolsBar.frame
        let toolsFrameY = toolsFrame.origin.y
        var toolsY = toolsFrameY
        toolsY -= offsetY
        let toolHeight = toolsBar.frame.height
        if offsetY > 0, -toolsY >= toolHeight {
            if toolsFrameY != -toolHeight {
                toolsFrame.origin.y = -toolHeight
                toolsBar.frame = toolsFrame
            }
            return
        }
        
        // 下滑: offsetY < 0
        if offsetY < 0, -toolsY <= 0 {
            if toolsFrameY != 0 {
                toolsFrame.origin.y = 0
                toolsBar.frame = toolsFrame
            }
            return
        }
        
        toolsFrame.origin.y = toolsY
        toolsBar.frame = toolsFrame
    }
}

// MARK: - 初始化设置
private extension STContentViewController {
    
    func setupMakeAddSubviews() {
        addChild(tabsView)
        view.addSubview(toolsBar)
        
        view.addSubview(tabsView.view)
    }
    
    func setupMakeLayoutSubviews() {
        let x: CGFloat = .zero
        let y: CGFloat = .zero
        let width: CGFloat = .dv_width
        let height: CGFloat = toolsBarHeight
        toolsBar.frame = CGRect(x: x, y: y, width: width, height: height)
        let colors = [UIColor.red.cgColor, UIColor.green.cgColor]
        toolsBar.gd_add(colors: colors, direction: .vertical)
        tabsView.view.snp.makeConstraints { make in
            make.top.equalTo(toolsBar.snp.bottom)
            make.left.right.bottom.equalTo(view)
        }
    }
    
    func setupMakeInitialize() {
        viewModel.commentsResponse = { [weak self] in
            self?.tabsView.reloadTabs()
        }
    }
}
